package com.example.question.bt.backtrack2;

import java.util.ArrayList;
import java.util.List;

/**
 * 二进制手表
 *
 * @link { <a href="https://leetcode.cn/problems/binary-watch/">...</a> }
 */
public class Code01_401 {
    public static void main(String[] args) {
        Code01_401 code = new Code01_401();
        List<String> list = code.readBinaryWatch(3);
        System.out.println(list);
    }

    public List<String> readBinaryWatch(int turnedOn) {
        int[] hours = {1, 2, 4, 8};
        int[] minutes = {1, 2, 4, 8, 16, 32};
        int h = Math.min(turnedOn, 4);
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<String> ret = new ArrayList<>();
        for (int i = 0; i <= h; i++) {
            list1.clear();
            list2.clear();
            process(hours, 0, 11, 0, i, 0, 0, list1);
            process(minutes, 0, 59, 0, turnedOn - i, 0, 0, list2);
            for (Integer hour : list1) {
                for (Integer min : list2) {
                    if (min <= 9) {
                        ret.add(hour + ":" + String.format("%02d", min));
                    } else {
                        ret.add(hour + ":" + min);
                    }
                }
            }
        }
        return ret;
    }

    private void process(int[] nums, int startIndex, int upper, int lower, int k, int count, int sum, List<Integer> ret) {
        if (count == k && sum <= upper && sum >= lower) {
            ret.add(sum);
            return;
        }
        if (sum > upper) {
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            // todo 思考一下 这个直接放在函数里面操作为什么不行  看看老汤是怎么讲解的这个题目
            sum += nums[i];
            count += 1;
            process(nums, i + 1, upper, lower, k, count, sum, ret);
            count -= 1;
            sum -= nums[i];
        }
    }


}
